@extends('admin.home.main')

@section('body')
<section id="main-content">
	  <section class="wrapper">
		<h3>教具包添加</h3>
		<!-- BASIC FORM ELELEMNTS -->
		<div class="row mt">
		  <div class="col-lg-12">
			<div class="form-panel">
			  <form class="form-horizontal style-form" action="/admin/teaching/tainsert" method="post" enctype="multipart/form-data" id="art_form">
			  	<input type="hidden" id="_token" name="_token" value="{{csrf_token()}}" readonly="readonly">
				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">教具包名称</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="aids_name" name="aids_name" placeholder="请输入教具包名称" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">教具包数量</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="aids_num" name="aids_num" placeholder="请输入教具包数量" size="50">
				  </div>
				</div>

                <div class="form-group">
                  <label class="col-sm-2 col-sm-2 control-label">教具数量</label>
                  <div class="col-sm-10">
                    <input type="text" style="width:400px;" class="form-control" id="aid_num" name="aid_num" placeholder="请输入教具数量" size="50">
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-sm-2 col-sm-2 control-label">库存数量</label>
                  <div class="col-sm-10">
                    <input type="text" style="width:400px;" class="form-control" id="stock_num" name="stock_num" placeholder="请输入库存数量" size="50">
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-sm-2 col-sm-2 control-label">教具包价格</label>
                  <div class="col-sm-10">
                    <input type="text" style="width:400px;" class="form-control" id="price" name="price" placeholder="请输入教具包价格" size="50">
                  </div>
                </div>

		<div class="form-group">

              <label class="col-sm-2 col-sm-2 control-label">教具包详情</label>

              <div class="col-sm-10">

                <a class="btn btn-theme02" href="javascript:;" onclick="uploadPhoto()">选择图片</a>

                <input type="file" id="material_file" style="display:none;" name="material_file" onchange="upload()">

                <div class="fileupload-new thumbnail" style="width: 200px; margin-top:10px;">

                    <img id="preview_photo" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="">

                </div>
		<input type="hidden" name="teaching_aids" id="teaching_aids" value="teaching_aids">
                <input type="hidden" id="imgsrc" name="imgsrc">

              </div>

            </div>
				
				<button type="button" id="save" class="btn btn-success">提交</button>
				<button type="reset" class="btn btn-primary">重置</button>
			  </form>
			</div>
		  </div>
		  <!-- col-lg-12-->
		</div>

		
	  </section>
	  <!-- /wrapper -->
	</section>
<script type="text/javascript">
    function uploadPhoto() {
        $("#material_file").click();
    }

    function upload(){
    	if($('#material_file').val() == ''){
    		alert('请选择图片');
    		return false;
    	}

    	var formData = new FormData($('#art_form')[0]);
    	$.ajax({
    		url:'/admin/teaching/upload_teach_aid',
    		type:'post',
    		data:formData,
    		dataType:'json',
    		contentType: false,
            processData: false,
            success:function(data){
            	if(data.code == 200){
            		$('#preview_photo').attr('src','https://boom-dog.oss-cn-zhangjiakou.aliyuncs.com/'+data.imgurl);
            		$('#imgsrc').val('https://boom-dog.oss-cn-zhangjiakou.aliyuncs.com/'+data.imgurl);
            		alert('上传成功');
            	}else{
            		alert('上传失败');
            	}
            	return false;
            }
    	})
    }
    $('#save').click(function(){
    	var _token = $('#_token').val(),
            aids_name = $('#aids_name').val(),
            aids_num = $('#aids_num').val(),
            aid_num = $('#aid_num').val(),
            aids_stock_num = $('#stock_num').val(),
            price = $('#price').val();

        if(!aids_name){
            alert('请输入教具包名称');
            return false;
        }

        if(!aids_num){
            alert('请输入教具包总数量');
            return false;
        }

        if(!aid_num){
            alert('请确认教具包下教具数量');
            return false;
        }

        if(!aids_stock_num){
            alert('请确认初始库存量');
            return false;
        }

        if(!price){
            alert('请输入教具包价格');
            return false;
        }

    	$.ajax({
    		url:'/admin/teaching/tainsert',
    		type:'post',
    		data:{'_token':_token,'name':aids_name,'num':aids_num,'aid_num':aid_num,'stock':aids_stock_num,'price':price},
    		dataType:'json',
    		success:function(data){
    			if(data.code == 200){
    				alert('教具包添加成功');
    				location.href="/admin/teaching/talist";
    			}else if(data.code == 300){
    				alert('教具包已存在请勿重复添加');
    				location.reload();
    			}else{
    				alert('教具包添加失败');
    				location.reload();
    			}
    		}
    	})
    })
</script>
@endsection
